package Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86156
 * Date: 2023-04-10
 * Time: 14:10
 */
public class UserTest {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<User>();
        Scanner sc = new Scanner(System.in);

        String[] str = new String[3];
        SIMS sims = new SIMS();

       loop: while (true) {
            System.out.println("---  欢迎来到学生管理系统  ---");
            System.out.println("请选择操作\n1登录\n2注册\n3忘记密码");
            int num = sc.nextInt();
            switch(num){
                case 1:
                    userLogin(list);
                    sims.main(str);
                    break;
                case 2:
                    list = userLogon(list);
                    print(list);
                    break;
                case 3:
                    System.out.println(' ');
                    break;
                default :
                    break loop;
            }
        }
    }
    public static void print(ArrayList<User> list){
        System.out.println("姓名"+"\t"+"密码"+"\t"+"电话号码"+"\t"+"身份证号");
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println(user.getUserName()+"\t"
            +user.getUserPassWord()+"\t"+
                    user.getUserNum()+"\t"+
                    user.getUserid());
        }
    }

    // 登录功能
    public static void userLogin(ArrayList<User> list){
        int count4 = 3;
        while (count4!=0) {
            // 键盘录入用户名
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            String username = sc.next();
            int count = 0;
            for (int i = 0; i < list.size(); i++) {
                User user = list.get(i);
                if(user.getUserName().equals(username)){
                    count++;
                    break;
                }
            }
            if(count==0){
                System.out.println("用户名未注册,请先注册");
                return;
            }

            // 键盘录入密码
            System.out.println("请输入密码");
            String password = sc.next();

            // 键盘录入验证码
            while (true) {
                boolean flag1 = false;
                boolean flag2 = false;

                // 长度为五
                System.out.println("请输入验证码");
                String pcc = sc.next();
                if(pcc.length() == 5){
                    flag1 = true;
                }else{
                    continue;
                }

                int count1 = 0;
                int count2 = 0;
                // 由4位大写或者小写字母和1位数字组成，同一个字母可重复
                for (int i = 0; i < pcc.length(); i++) {
                    if(pcc.charAt(i)>='a'&&pcc.charAt(i)<='z'){
                        count1++;
                    }else if(pcc.charAt(i)>='A'&&pcc.charAt(i)<='Z'){
                        count1++;
                    }else if(pcc.charAt(i)>='0'&&pcc.charAt(i)<='9'){
                        count2++;
                    }
                }
                if(count1 == 4&& count2 == 1){
                    flag2 =true;
                }else{
                    System.out.println("验证码由4位大写或者小写字母和1位数字组成，同一个字母可重复");
                    continue;
                }
                if(flag1&&flag2){
                    break;
                }
            }
            //再判断用户名和密码是否正确，有3次机会
            for (int i = 0; i < list.size(); i++) {
                User user = list.get(i);
                if(user.getUserName().equals(username)  && user.getUserPassWord().equals(password)){
                    System.out.println("登陆成功");
                    return;
                }
            }
            System.out.println("用户名或密码不正确,你还有"+(count4-1)+"次机会");
            count4--;
        }
    }



    // 注册功能
    public static ArrayList<User> userLogon(ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
        User user = new User();

        while (true) {
            System.out.println("请输入用户名:");
            String username = sc.next();

            boolean flag1 = false;
            boolean flag2 = false;
            boolean flag3 = false;
            //用户名唯一
            int count = 0;
                for (int i = 0; i < list.size(); i++) {
                    user = list.get(i);
                    if(username.equals(user.getUserName())){
                        System.out.println("用户名已经存在,请换一个");
                       count++;
                    }
                }
                if(count!=0){
                    continue;
                }
                    flag1 = true;

            // 用户名长度必须在3~15位之间
                if(username.length() < 3 || username.length() > 15){
                    System.out.println("用户名长度必须在3~15位之间,请重新输入");
                    continue;
                }else{
                    flag2 = true;
                }

            // 只能是字母加数字的组合，但是不能是纯数字
                int count1 = 0;
                int count2 = 0;
                for (int i = 0; i < username.length(); i++) {
                    if(username.charAt(i)>'a' && username.charAt(i)<'z'){
                        count1++;
                    } else if (username.charAt(i)>'A' && username.charAt(i)<'Z') {
                        count1++;
                    } else if (username.charAt(i)>'0' && username.charAt(i)<'9') {
                        count2++;
                    }
                }
                if(count1<=0 || count2<=0){
                    System.out.println("只能是字母加数字的组合，但是不能是纯数字,请重新输入");
                    continue ;
                }else{
                    flag3 = true;
                }

                user.setUserName(username);

                if(flag1&&flag2&&flag3){
                    break;
            }
        }

        // 密码键盘输入两次，两次一致才可以进行注册。
        while (true) {
            System.out.println("请输入密码:");
            String userPassword1 = sc.next();
            System.out.println("请确认密码:");
            String userPassword2 = sc.next();
            if(userPassword1.equals(userPassword2)){
                System.out.println("密码正确");
                user.setUserPassWord(userPassword1);
                break;
            }else{
                System.out.println("密码错误,请重新输入");
                continue;
            }
        }
        while (true) {
            boolean flag1 = false;
            boolean flag3 = false;
            boolean flag2 = false;
            boolean flag4 = false;
            // 输入身份证号码
            System.out.println("请输入身份证号码:");
            String userid = sc.next();

           // 长度必须为十八位
                if(userid.length()==18){
                    flag1 = true;
                }else{
                    System.out.println("长度必须为十八位,不符合要求,请重新输入");
                    continue;
                }

            // 不能以零开头
            if(userid.charAt(0)==0){
                System.out.println("id不能以零开头,请重新输入");
                continue;
            }else{
                flag2 = true;
            }

            // 前十七位,必须都是数字
                int count = 0;
                for (int i = 0; i < userid.length()-1; i++) {
                    if(userid.charAt(i)>='0' && userid.charAt(i)<='9'){
                        count++;
                    }
                }
                if(count == 17){
                    flag3 = true;
                }else{
                    System.out.println("前十七位,必须都是数字,请重新输入");
                    continue;
                }

                // 最为一位可以是数字，也可以是大写X或小写x
                if(userid.charAt(userid.length()-1) == 'x'){
                    flag4 = true;
                } else if (userid.charAt(userid.length()-1) == 'X') {
                    flag4 = true;
                }else if(userid.charAt(userid.length()-1)>='0'||userid.charAt(userid.length()-1)<='9'){
                    flag4 = true;
                }

                if( flag1 && flag2 && flag3 && flag4){
                    user.setUserid(userid);
                    break;
                }
           }
        //手机号验证。
        while(true){
            boolean flag1 = false;
            boolean flag3 = false;
            boolean flag2 = false;
            System.out.println("请输入手机号:");
            String num = sc.next();

            // 长度为十一位
           if(num.length() == 11){
               flag1 = true;
           }else{
               System.out.println("长度不符合11位,请重新输入");
               continue;
           }

           // 不能以0为开头
             if(num.charAt(0) == '0'){
                 System.out.println("不能以0为开头");
             }else{
                 flag2 =true;
             }

             // 必须都是数字
            int count = 0;
            for (int i = 0; i < num.length(); i++) {
                if(num.charAt(i)>='0'&& num.charAt(i)<='9'){
                    count++;
                }
            }
            if(count==11){
                flag3 = true;
            }else{
                continue;
            }

           if(flag1&&flag2&&flag3){
               user.setUserNum(num);
               break;
           }
        }
        list.add(user);
        return list;
    }
}
